/*
 * Copyright 2011 Philippe Blanc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package me.l1k3.fx.client.channel;

import me.l1k3.core.client.Color;
import me.l1k3.fx.client.channel.impl.ColorChannelImplJS;
import me.l1k3.fx.client.channel.inter.Channel;
import me.l1k3.fx.client.channel.inter.ChannelContext;
import me.l1k3.fx.client.channel.trait.Trait;
import me.l1k3.fx.client.engine.inter.FXEngine;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style;

public abstract class ColorChannel implements Channel {
    public static final String COLOR = "color";
    public static final String BACKGROUND = "backgroundColor";

    protected String type;
    protected boolean backward;
    protected Trait red = new Trait();
    protected Trait green = new Trait();
    protected Trait blue = new Trait();

	//- - - factory
	
    public final static ColorChannel create(String type) {
        if (type.equals(FXEngine.JS)) {
            return new ColorChannelImplJS();
        }
        
        return GWT.create(ColorChannel.class);
    }
    
	public final static ColorChannel range(String type, Color start, Color end) {
		ColorChannel channel = create(type);
		channel.setRange(COLOR, start, end);
		
		return channel;
	}

	public final static ColorChannel range(String type, int r, int g, int b, int rr, int gg, int bb) {
		ColorChannel channel = create(type);
		channel.setRange(COLOR, r, g, b, rr, gg, bb);
		
		return channel;
	}
	
	public final static ColorChannel rangeBackground(String type, Color start, Color end) {
		ColorChannel channel = create(type);
		channel.setRange(BACKGROUND, start, end);
		
		return channel;
	}

	public final static ColorChannel rangeBackground(String type, int r, int g, int b, int rr, int gg, int bb) {
		ColorChannel channel = create(type);
		channel.setRange(BACKGROUND, r, g, b, rr, gg, bb);
		
		return channel;
	}
	
	//- - -
	
    public void setRange(String type, Color start, Color end) {
    	setRange(type, start.getRed(), start.getGreen(), start.getBlue(), end.getRed(), end.getGreen(), end.getBlue());
    }
	
    public void setRange(String type, int r, int g, int b, int rr, int gg, int bb) {
        this.type = type;
        red.set(r, rr);
        green.set(g, gg);
        blue.set(b, bb);
	}
    
    protected void setColor(Style style) {
        style.setProperty(type, Color.toRGB((int)red.get(backward),(int)green.get(backward),(int)blue.get(backward)));
    }
    
    //- - -
    
    @Override
    public boolean isForward() {
        return !backward;
    }    
    
    @Override
    public void init(ChannelContext context) {
        setColor(context.getStyle());
    }
    
    @Override
    public void toggle(ChannelContext context) {
        backward = !backward;
        setColor(context.getStyle());
    }
    
    /*
    @Override
    public void start(ChannelContext context) { }
    */
    
    /*
    @Override
    public void update(ChannelContext context) { }
    */

	@Override
	public void complete(ChannelContext context) {
	    toggle(context);
	}
}
